<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 注解详解：@RequestBody 与 @ResponseBody</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        pre code {
            display: block;
            padding: 20px;
            overflow-x: auto;
            line-height: 1.6;
        }
        .keyword { color: #569cd6; }
        .string { color: #ce9178; }
        .annotation { color: #dcdcaa; }
        .method { color: #4ec9b0; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 3rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider i {
            position: relative;
            background: white;
            padding: 0 1rem;
            color: #667eea;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                Spring 注解深度解析
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                掌握 @RequestBody 与 @ResponseBody 的精髓
            </p>
            <div class="flex justify-center space-x-4 text-sm">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-code mr-2"></i>RESTful API
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-exchange-alt mr-2"></i>数据转换
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-server mr-2"></i>Spring Framework
                </span>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="max-w-4xl mx-auto px-6 py-16">
        <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap serif-font">在</span>现代 Web 开发中，<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@RequestBody</code> 和 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ResponseBody</code> 是 Spring 框架中处理 HTTP 请求和响应的核心注解。它们在 RESTful API 开发中扮演着至关重要的角色，实现了请求数据的自动绑定和响应数据的智能转换。
            </p>
        </div>
    </section>

    <!-- Visual Diagram -->
    <section class="max-w-6xl mx-auto px-6 pb-16">
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <h2 class="text-3xl font-bold text-center mb-8 gradient-text">数据流转示意图</h2>
            <div class="mermaid">
                graph LR
                    A[客户端] -->|HTTP Request + JSON| B[@RequestBody]
                    B --> C[Java Object]
                    C --> D[Controller Method]
                    D --> E[Java Object]
                    E --> F[@ResponseBody]
                    F -->|HTTP Response + JSON| G[客户端]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style G fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style F fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style D fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="max-w-6xl mx-auto px-6 pb-16">
        <div class="grid md:grid-cols-2 gap-8">
            <!-- @RequestBody Card -->
            <div class="bg-white rounded-2xl shadow-xl overflow-hidden card-hover">
                <div class="hero-gradient p-6 text-white">
                    <h2 class="text-3xl font-bold flex items-center">
                        <i class="fas fa-download mr-3"></i>
                        @RequestBody
                    </h2>
                </div>
                <div class="p-8">
                    <div class="mb-6">
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">
                            <i class="fas fa-bullseye text-purple-600 mr-2"></i>核心作用
                        </h3>
                        <p class="text-gray-600 leading-relaxed">
                            将 HTTP 请求体中的数据自动绑定到方法参数上，支持 JSON、XML 等多种格式的数据转换为 Java 对象。
                        </p>
                    </div>
                    
                    <div class="mb-6">
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">
                            <i class="fas fa-lightbulb text-purple-600 mr-2"></i>使用场景
                        </h3>
                        <p class="text-gray-600 leading-relaxed">
                            当客户端发送复杂的数据结构时，如 JSON 格式的用户信息、订单数据等，@RequestBody 能够自动完成反序列化工作。
                        </p>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <i class="fas fa-code mr-2"></i>示例代码
                        </div>
                        <pre><code><span class="annotation">@PostMapping</span>(<span class="string">"/user"</span>)
<span class="keyword">public</span> ResponseEntity&lt;User&gt; <span class="method">createUser</span>(<span class="annotation">@RequestBody</span> User user) {
    <span class="comment">// user 参数将包含从请求体中解析出的 User 对象</span>
    <span class="keyword">return</span> ResponseEntity.ok(user);
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- @ResponseBody Card -->
            <div class="bg-white rounded-2xl shadow-xl overflow-hidden card-hover">
                <div class="hero-gradient p-6 text-white">
                    <h2 class="text-3xl font-bold flex items-center">
                        <i class="fas fa-upload mr-3"></i>
                        @ResponseBody
                    </h2>
                </div>
                <div class="p-8">
                    <div class="mb-6">
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">
                            <i class="fas fa-bullseye text-purple-600 mr-2"></i>核心作用
                        </h3>
                        <p class="text-gray-600 leading-relaxed">
                            将 Controller 方法的返回值直接写入 HTTP 响应体，自动将 Java 对象序列化为 JSON、XML 等格式。
                        </p>
                    </div>
                    
                    <div class="mb-6">
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">
                            <i class="fas fa-lightbulb text-purple-600 mr-2"></i>使用场景
                        </h3>
                        <p class="text-gray-600 leading-relaxed">
                            在 RESTful API 中返回数据时，@ResponseBody 能够自动将返回的 Java 对象转换为客户端期望的格式。
                        </p>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <i class="fas fa-code mr-2"></i>示例代码
                        </div>
                        <pre><code><span class="annotation">@GetMapping</span>(<span class="string">"/user/{id}"</span>)
<span class="annotation">@ResponseBody</span>
<span class="keyword">public</span> User <span class="method">getUser</span>(<span class="annotation">@PathVariable</span> Long id) {
    <span class="comment">// 返回的 User 对象将被转换为 JSON 并写入响应体</span>
    <span class="keyword">return</span> userService.findById(id);
}</code></pre>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Section Divider -->
    <div class="section-divider max-w-6xl mx-auto">
        <i class="fas fa-link text-2xl"></i>
    </div>

    <!-- Best Practice Section -->
    <section class="max-w-6xl mx-auto px-6 pb-16">
        <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-2xl p-8 md:p-12">
            <h2 class="text-3xl font-bold text-center mb-8 gradient-text">最佳实践：@RestController</h2>
            
            <div class="bg-white rounded-xl p-6 mb-8 shadow-lg">
                <p class="text-lg text-gray-700 leading-relaxed mb-4">
                    <i class="fas fa-info-circle text-purple-600 mr-2"></i>
                    在实际开发中，我们通常使用 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@RestController</code> 注解，它是 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@Controller</code> 和 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ResponseBody</code> 的组合注解。
                </p>
                <p class="text-gray-600">
                    使用 @RestController 后，Controller 中的所有方法返回值都会自动通过 @ResponseBody 处理，无需在每个方法上单独添加。
                </p>
            </div>

            <div class="code-block">
                <div class="code-header">
                    <i class="fas fa-star mr-2"></i>综合使用示例
                </div>
                <pre><code><span class="annotation">@RestController</span>
<span class="keyword">public class</span> <span class="method">UserController</span> {

    <span class="annotation">@PostMapping</span>(<span class="string">"/user"</span>)
    <span class="keyword">public</span> User <span class="method">createUser</span>(<span class="annotation">@RequestBody</span> User user) {
        <span class="comment">// 使用 @RequestBody 读取请求体并转换为 User 对象</span>
        <span class="keyword">return</span> user;
    }

    <span class="annotation">@GetMapping</span>(<span class="string">"/user/{id}"</span>)
    <span class="keyword">public</span> User <span class="method">getUser</span>(<span class="annotation">@PathVariable</span> Long id) {
        <span class="comment">// 使用 @ResponseBody 将返回的 User 对象转换为 JSON 并写入响应体</span>
        <span class="keyword">return</span> userService.findById(id);
    }
}</code></pre>
            </div>
        </div>
    </section>

    <!-- Key Points Summary -->
    <section class="max-w-6xl mx-auto px-6 pb-16">
        <h2 class="text-3xl font-bold text-center mb-12 gradient-text">核心要点总结</h2>
        <div class="grid md:grid-cols-3 gap-